package iosr11.ui.data;

import iosr11.Service.ServiceLocatorImpl;
import iosr11.Service.StudentService;
import iosr11.data.Student;
import iosr11.data.User;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.component.html.HtmlDataTable;
import javax.faces.context.FacesContext;

;


public class StudentListBean {
	private Logger logger = Logger.getLogger("iosr.ui.data");
    private List<Student> studentList;
	private StudentService service;
	private HtmlDataTable dataTable;
    
    
	public StudentListBean(){
		this.service = (StudentService)ServiceLocatorImpl.getInstance().getService(Student.class);
		//this.ensureListNotNull();
		if(this.service==null){
			logger.severe("Could not obtain reference to StudentService");
			return;
		}
	}
	
	/*private void ensureListNotNull(){
		if(this.studentList==null){
			if(this.service != null){
				this.studentList = service.getAllStudents();
				if(this.studentList == null){
					this.studentList = new ArrayList<Student>();
				}
			}else{
				logger.severe("Could not obtain reference to StudentService");
				return;
			}
		}
	}*/
	
	public List<Student> getAllStudents(){
		//ensureListNotNull();
		if(this.service != null){
			this.studentList = service.getAllStudents();
		}
		if(this.studentList == null){
			this.studentList = new ArrayList<Student>();
		}
		return this.studentList;
	}
	
	
	public HtmlDataTable getDataTable(){
		return this.dataTable;
	}
	public void setDataTable(HtmlDataTable table){
		this.dataTable = table;
	}
	
		
	private void saveSelectedStudentId(){
		 Student selectedStudent = (Student) dataTable.getRowData();
		 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(
				 SessionMapKeys.SelectedStudentEmail.name(), selectedStudent.getEmail());
	}
	
	public String goToStudentDetails(){
		 saveSelectedStudentId();
		 return "showStudent";
	} 
	
	public String editStudentMarks(){
		 saveSelectedStudentId();
		 return "editStudentMarks";
	} 
	
	public String editStudentDetails(){
		 saveSelectedStudentId();
		 return "editStudent";
	} 

	public String addStudentDetails(){
		 FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SessionMapKeys.SelectedStudentEmail.name());
		 return "editStudent";
	} 

	
}


